package com.smartproxy.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class SPConnectionHandler extends Thread {

	private Socket from;
	private Socket to;
	
	public SPConnectionHandler( Socket from, Socket to ) {
		this.from = from;
		this.to = to;
		
		this.start();
	}
	
	public void run() {
		
		try {
			DataInputStream dis = new DataInputStream( this.from.getInputStream() );
			DataOutputStream dos = new DataOutputStream( this.to.getOutputStream() );
			byte[] buffer = new byte[ 1500 ];
			int size = 0;
						
			while( size != -1 ) {
				size = dis.read( buffer );
				System.out.println( new String( buffer ) );
				System.out.println( size );
				if( size != -1 ) {
					dos.write( buffer, 0, size );
				}
				
				
				//dos.write( dis.readByte() );
				//dos.writeUTF( dis.readUTF() );
			}
			
			

			/*BufferedReader br = new BufferedReader( new InputStreamReader( this.from.getInputStream() ) );
			PrintWriter pw = new PrintWriter( this.to.getOutputStream(), true );

			while( true ) {
				String data = br.readLine();
				
				System.out.println( data );
				
				pw.println( data );
			}*/

			
			/*BufferedReader br = new BufferedReader( new InputStreamReader( this.from.getInputStream() ) );
			BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( this.to.getOutputStream() ) );

			while( true ) {
				String data = br.readLine();
				
				System.out.println( data );
				
				bw.write( data + "\n" );
				bw.flush();
			}*/

		} catch (IOException e) {
			System.out.println( "Se acabó" );
		}
	}
}
